import { Collection } from "mongodb";
import { DBclient } from "../utils/db";
import { Random } from "../utils/random";
const db = new DBclient();

export class Index extends Random {
  _coll: Promise<Collection<any>> | any
  _table: string
  constructor(table: string) {
    super();
    this._table = table
  }
  async _getcoll() {
    if (!this._coll) {
      this._coll = db.getCollection(this._table)
    }
    return this._coll;
  }
  /**
   * 查询符合条件的数据总数
   * @param query 查询条件
   * @returns 总条数
   */
  async _count(query = {}): Promise<number> {
    const coll = await this._getcoll();
    const data = await coll.countDocuments(query);
    return data
  }


  /**
   * 根据条件获取数据
   * @param body 查询条件
   * @returns 查询结果
   */
  async _find(params: object): Promise<Array<object>> {
    const coll = await this._getcoll();
    const data = await coll.find(params).toArray();
    return data
  }
  /**
   * 根据条件获取分页数据
   * @param body 查询条件
   * @returns 查询结果
   */
  async _query(body: { page: any, query: any }): Promise<object> {
    let { page: { size, page }, query = {} } = body;
    const coll = await this._getcoll();
    const data = await coll.find(query).skip(--page * size).limit(size - 0).toArray();
    const total = await this._count(query);
    return { page: ++page, size: size - 0, total, data }
  }
  /**
   * 添加数据
   * @param params 传入参数
   * @returns 添加状态
   */
  async _add(params: object): Promise<boolean> {
    const coll = await this._getcoll();
    const id = await this.ID(coll);
    const { result: { ok, n } } = await coll.insertOne({ ...params, id });
    return !!(ok && n);

  }

  /**
   * 更新数据
   * @param params 更新数据
   * @returns 更新状态
   */
  async _edit(params: { id: number, [p: string]: any }): Promise<boolean> {
    const coll = await this._getcoll();
    const { id, ...items } = params;
    const { result: { ok, n } } = await coll.updateOne({ id: id - 0 }, { $set: items });
    return !!(ok && n);
  }
  /**
   * 删除数据
   * @param id 数据id
   * @returns 删除状态
   */
  async _del(id: number | Array<number>): Promise<boolean> {
    if (typeof id === 'number') id = [id];
    const coll = await this._getcoll();
    const { result: { ok, n } } = await coll.deleteMany({ id: { $in: id } });
    return !!(ok && n);
  }


}
export class Token extends Random {
  _coll: Promise<Collection<any>> | any
  _table: string
  constructor(table: string) {
    super();
    this._table = table
  }
  async _getcoll() {
    if (!this._coll) {
      this._coll = db.getCollection(this._table)
    }
    return this._coll;
  }
  async _clearToken(username?: string) {
    // 删除超时和指定token，
    const coll = await this._getcoll();
    const { result: { ok, n } } = await coll.deleteMany({ $or: [{ maxAge: { $lt: Date.now() } }, { username }, { token: username }] });
    return !!(ok && n)
  }
  async _getToken(token: any) {
    const coll = await this._getcoll();
    const data = await coll.find(token).toArray();
    return data;
  }
  async _setToken(params: object) {
    const coll = await this._getcoll();
    const id = await this.ID(coll);
    const { result: { ok, n } } = await coll.insertOne({ ...params, id })
    return !!(ok && n)
  }
}

